package codingforgreat.lchot150;

public class Lc130 {
    public void solve(char[][] board) {
        int N = board.length;
        int M = board[0].length;
        for(int i = 0;i < N;i++){
            if(board[i][0] == 'O'){
                infect(board,i,0);
            }
            if(board[i][M - 1] == 'O'){
                infect(board,i,M - 1);
            }
        }
        for(int i = 0;i < M;i++){
            if(board[0][i] == 'O'){
                infect(board,0,i);
            }
            if(board[N - 1][i] == 'O'){
                infect(board,N - 1,i);
            }
        }
        for(int i = 0;i < N;i++){
            for(int j = 0;j < M;j++){
                if(board[i][j] == 'F'){
                    board[i][j] = 'O';
                }else if(board[i][j] == 'O'){
                    board[i][j] = 'X';
                }
            }
        }
    }
    public void infect(char[][] board,int i ,int j){
        int N = board.length;
        int M = board[0].length;
        if(i < 0 || i >= N || j <0 || j >= M ){
            return;
        }
        if(board[i][j] != 'O'){
            return;
        }
        if(board[i][j] == 'O'){
            board[i][j] = 'F';
        }
        infect(board,i + 1,j);
        infect(board,i - 1,j);
        infect(board,i,j + 1);
        infect(board,i,j - 1);
    }
}
